package org.shinetour.sms.model;

/**
 * @Description:
 * @Author: ZhenHua Wang
 * @CreateDate: 2020/3/31 17:31
 */

public class ResultModel<T> {
    /**
     * 错误代码，对应{@link StandardError}。success=200
     */
    private Integer code;
    /**
     * 快捷判断操作是否成功
     */
    private Boolean success;
    /**
     * 错误信息
     */
    private String message;
    /**
     * 返回结果
     */
    private T data;

    /**
     * @Description: 默认操作成功
     * @Author: ZhenHua Wang
     * @CreateDate: 2018/10/30 17:31
     */
    public ResultModel<T> success() {
        this.code = StandardError.SUCCESS.getCode();
        this.message = StandardError.SUCCESS.getMsg();
        return this;
    }

    /**
     * @Description: 带自定义返回信息的操作成功
     * @Author: ZhenHua Wang
     * @CreateDate: 2018/10/30 17:31
     */
    public ResultModel<T> success(String successMsg) {
        this.code = StandardError.SUCCESS.getCode();
        this.message = successMsg;
        return this;
    }

    /**
     * @Description: 带返回值的操作成功
     * @Author: ZhenHua Wang
     * @CreateDate: 2018/10/30 17:31
     */
    public ResultModel<T> success(T data) {
        this.code = StandardError.SUCCESS.getCode();
        this.message = StandardError.SUCCESS.getMsg();
        this.data = data;
        return this;
    }

    /**
     * @Description: 通用错误
     * @Author: ZhenHua Wang
     * @CreateDate: 2018/10/30 18:20
     */
    public ResultModel<T> error() {
        this.code = StandardError.ERROR.getCode();
        this.message = StandardError.ERROR.getMsg();
        return this;
    }

    /**
     * @Description: 带返回值错误
     * @Author: ZhenHua Wang
     * @CreateDate: 2018/10/30 18:20
     */
    public ResultModel<T> error(T data) {
        this.code = StandardError.ERROR.getCode();
        this.message = StandardError.ERROR.getMsg();
        this.data = data;
        return this;
    }

    /**
     * @Description: 带信息的通用错误
     * @Author: ZhenHua Wang
     * @CreateDate: 2018/11/6 18:18
     */
    public ResultModel<T> error(String msg) {
        this.code = StandardError.ERROR.getCode();
        this.message = msg;
        return this;
    }

    /**
     * @Description: 标准类型错误
     * @Author: ZhenHua Wang
     * @CreateDate: 2018/10/30 18:22
     */
    public ResultModel<T> error(StandardError error) {
        this.code = error.getCode();
        this.message = error.getMsg();
        return this;
    }

    /**
     * @Description: 标准类型错误，含自定义信息
     * @Author: ZhenHua Wang
     * @CreateDate: 2018/10/30 18:24
     */
    public ResultModel<T> error(StandardError error, String errorDetail) {
        this.code = error.getCode();
        this.message = error.getMsg() + ":" + errorDetail;
        return this;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Boolean isSuccess() {
        return this.code.equals(StandardError.SUCCESS.getCode());
    }
}
